package com.itheima.controller.permission;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.permission.PermissionService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

/**
 * @author 陈功炳
 * @since 2021/11/5 16:04
 */
@RestController
@RequestMapping("permission")
public class PermissionController {
    @Reference
    private PermissionService permissionService;

    /**
     *权限管理 分页查询
     * @param queryPageBean 前端的page size和查询条件
     * @return 返回total(总记录数)和list(当前页结果)
     */
    @PostMapping("findPage")
    private PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        return permissionService.findByPage(queryPageBean);
    }

    /**
     *权限管理 添加功能
     * @param permission 前端输入的数据permission
     * @return 返回flag前端进行判断
     */
    @PostMapping("add")
    public Result add(@RequestBody Permission permission){
        Boolean flag = permissionService.add(permission);
        return new Result(flag,flag? MessageConstant.ADD_PERMISSION_SUCCESS:MessageConstant.ADD_PERMISSION_FAIL);
    }

    /**
     * 权限管理 根据id查询
     * @param id 点击查询按钮传来对应数据的id
     * @return 返回permission数据
     */
    @GetMapping("findById")
    public Result findById(Integer id){
        Permission permission = permissionService.findById(id);
        return new Result(permission!=null,permission!=null?MessageConstant.EDIT_PERMISSION_SUCCESS:MessageConstant.EDIT_PERMISSION_FAIL,permission);
    }

    /**
     * 权限管理 修改功能
     * @param permission 将前端修改数据装到permission
     * @return 返回flag前端进行判断
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Permission permission){
        Boolean flag = permissionService.edit(permission);
        return new Result(flag,flag?MessageConstant.EDIT_PERMISSION_SUCCESS:MessageConstant.EDIT_PERMISSION_FAIL);
    }

    /**
     * 权限管理 删除功能
     * @param id 点击删除的那条数据传来的id
     * @return 返回flag给前端判断
     */
    @GetMapping("delete")
    public Result delete(Integer id){
        Boolean flag = permissionService.delete(id);
        return new Result(flag,flag?MessageConstant.DELETE_PERMISSION_SUCCESS:MessageConstant.DELETE_PERMISSION_FAIL);
    }
}
